<?php
class model_message extends framework_base_model {

	private static $table = 'weixin_keywords_message';
	
	
	/**
	 * 添加关键词
	 * @param array $data
	 * @return number
	 */
	public static function messageAdd($data) {
    	if(!is_array($data)) {
    		return 0;
    	}
    	try{
    		$sql = "INSERT INTO ".self::$table." SET ";
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$sql .= 'raw_add_time=NOW(),raw_update_time=now()';
    		$arr=self::DB() -> dbh -> prepare($sql);
    		$rel = $arr->execute($binds);
    		if($rel == TRUE) {
    			return self::DB()->dbh->lastInsertId();
    		} else {
    			return 0;
    		}
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
    }
    /*
     * 根据ID获取内容
     */
    public static function getmessageInfoById($id) {
    	$info = array();
    	if(intval($id) <= 0) {
    		return $info;
    	}
    	$sql = "select * from ".self::$table." where id=:id and delete_flag = 0";
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute(array(":id" => intval($id)));
    		$info = $st -> fetch();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
    }
    /**
     * 更新消息内容
     * @param unknown $id
     * @param unknown $data
     * @return unknown|number
     */
    public static function updatemessageInfo($id,$data) {
    	try{
    		$sql = 'UPDATE '.self::$table.' SET ';
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$ids = implode(',', $id);
    		$sql = trim($sql,',');
    		$sql .= ' WHERE `id` in ('.$ids.')';
    		$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute($binds);
    		$rowCount = $stm -> rowCount();
    		return $rowCount;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return -1;
    	}
    }
    /**
     * 获取列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function messageList($nextNum = 0,$pagesize = 0,$condition = array()) {
    	$sql = "SELECT * FROM `".self::$table . "` WHERE delete_flag = 0 ";
    	if(is_array($condition)&& count($condition)>0){
    		$keywords = trim($condition['keyword']);
    		if(!empty($keywords)) {
    			$sql .= " AND `keywords` like '%".$keywords."%'";    			
    		}
			$id = trim($condition['id']);
    		if(!empty($id)) {
    			$sql .= " AND `id` = {$id}";    			
    		}
    	}
    	$sql .= " ORDER BY `id` DESC";
    	
        if($pagesize != 0 ){
    		$sql .= " LIMIT $nextNum,$pagesize";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return $result;
    	}
		
		$sql = "select count(*) FROM " . self::$table . " WHERE `delete_flag` = 0";
   		if(is_array($condition)&& count($condition)>0){
    		$keywords = trim($condition['keyword']);
   			if(!empty($keywords)) {
    			$sql .= " AND `keywords` like '%".$keywords."%'";    			
    		}
    	}
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute();
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	/*
	* @desc 获取规则名称
	* @author kevin wang
	* @param void
	* @return array
	**/
	public static function get_meg_title(){
		$sql = "select `id`,`rule_name` from `weixin_keywords_message` where `delete_flag` = 0";
		$st = self::DB()->dbh->prepare($sql);
    	$st->execute();
    	$result = $st->fetchAll();
		return $result;
	}
}
